package com.igs.dao;

import java.sql.Connection;
import java.util.Hashtable;

import com.igs.dao.interfaces.IConnectionManager;

public class TestGuaConManager implements IConnectionManager {

	private static final long serialVersionUID = 1L;

	protected String dsName;
	protected Connection conn;
	protected boolean isTrx;
	protected boolean isRollback;

	public void begin() {
		isTrx = true;
	}

	public void close() {
	}

	public void end() {
		try {
			if (conn != null) {
				try {
					if (isRollback) {
					} else {
						conn.commit();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					conn.setAutoCommit(true);
					conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} finally {
			isTrx = false;
			isRollback = false;
			conn = null;
		}
	}

	public void setDataSource(String dsName) {
		this.dsName = dsName;
	}

	public Object getConnection() throws Exception {
		return getConnection("java:/comp/env/jdbc/ecos");
	}

	public Object getConnection(String dsName) throws Exception {
		if (dsName != null) {
			this.dsName = dsName;
		}

		if (this.dsName != null) {
			Connection connection;
			try {
				if (conn == null) {
					conn = null;
				}
				if (isTrx)
					conn.setAutoCommit(false);
				connection = conn;
			} catch (Exception ne) {
				throw ne;
			}
			return connection;
		}
		return null;
	}

	public Object getConnection(Hashtable env) throws Exception {
		return null;
	}

	public boolean isTrx() {
		return isTrx;
	}

	public void setRollback() {
		isRollback = true;
	}

	protected void finalize() throws Throwable {
		if (conn != null) {
			close();
		}
		super.finalize();
	}
}
